<template>
  <!-- 医生搜索 -->
  <el-select
    :value="value"
    :disabled="disabled"
    filterable
    remote
    clearable
    :remote-method="getSearch"
    @focus="getData"
    @change="onChange"
    @clear="onClear"
    placeholder="请输入医生姓名"
  >
    <el-option
      v-for="(item,index) in doctorArray"
      :key="index"
      :value="item.id"
      :label="item.trueName"
    ></el-option>
    <br />
    <pager v-model="pager" small @change-pagenum="getData"></pager>
  </el-select>
</template>
<script>
export default {
  props: {
    value: {
      type: [String, Number],
      default: "",
    },
    deptId: {
      type: [String, Number],
      default: "",
    },
    keyword: {
      type: String,
      default: "",
    },
    pageSize: {
      type: Number,
      default: 10,
    },
    disabled: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      doctorArray: [],
      name: "",
      pager: { pageSize: this.pageSize, pageNum: 1, total: 0 },
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    getSearch(name) {
      const th = this;
      th.pager.pageNum = 1;
      th.name = name;
      th.getData();
    },
    getData() {
      let th = this;
      //判断value是医生名称还是医生id
      let doctorId = typeof this.value == "string" ? "" : this.value;
      let j = {
        hpId: th.adminUser.hpId,
        deptId: th.deptId,
        doctorId: doctorId,
        doctorName: th.name,
        status: 1,
      };
      Object.assign(j, th.pager);
      th.$api.getDoctorPageList(j).then((res) => {
        if (res.executed) {
          th.doctorArray = res.dataBody.list;
          th.pager.total = res.dataBody.total;
        }
      });
    },
    onChange(value) {
      let th = this;
      th.$emit("input", value);
      th.$emit("change", value);
    },
    onClear() {
      let th = this;
      th.name = "";
      th.$emit("input", "");
      th.$emit("clear");
      th.getData();
    },
  },
  watch: {
    keyword: {
      immediate: true,
      handler: function (n, o) {
        let th = this;
        th.getSearch(n);
      },
    },
    value(n) {
      let th = this;
      if (n === "") {
        th.getSearch();
      }
    },
  },
};
</script>